﻿<div class="k-block divToolbar" style="margin: 5px;">
    <div style="display: inline-block;">
        <input id="txtWarehouse" data-value="0" />
    </div>
    <div style="display: inline-block; vertical-align: top">
        <input id="txtCustomer" data-value="0" style="width: 300px" />
    </div>
    <div style="display: inline-block; vertical-align: top">
        <input id="txtProduct" data-value="0" style="width: 300px" />
    </div>
    <br />
    <br />
    <input type="text" placeholder="请输入单据号" id="txtKey" class="k-textbox" />
    <div style="display: inline-block;">
        <select id="ddlMethod">
            <option value="0">全部单据</option>
            <option value="11">入库单</option>
            <option value="12">出库单</option>
            <option value="13">盘存单</option>
            <option value="14">调拨单</option>
        </select>
    </div>

    <div style="display: inline-block;">
        <input id="txtOrderDateBefore" class="date" placeholder="开始时间" />
    </div>
    --
    <div style="display: inline-block;">
        <input id="txtOrderDateAfter" class="date" placeholder="结束时间" />
    </div>
    <br />
    <br />
    <button class="k-button" onclick="FilterOrders()"><span class="k-icon k-i-search"></span><span>搜索</span></button>
    <a class="k-button" onclick="ExportOrders()"><span class="k-icon k-i-calendar"></span><span>导出单据</span></a>
</div>
<div class="ld-tabs">
    <ul>
        <li class="k-state-active">单据列表</li>
        <li>明细列表</li>
    </ul>
    <div>
        <div id="grid_orders" style="text-align: center"></div>
    </div>
    <div>
        <div id="grid_details" style="text-align: center"></div>
    </div>
    <form id="productForm" method="post" action="/Orders/ProductOrder/Export" target="_blank" style="display: none">
        <input type="hidden" name="orderNo" id="df_OrderNo" />
        <input type="hidden" name="customerID" id="df_CustomerID" />
        <input type="hidden" name="orderMethod" id="df_OrderMethod" />
        <input type="hidden" name="orderDateBefore" id="df_OrderDateBefore" />
        <input type="hidden" name="orderDateAfter" id="df_OrderDateAfter" />
        <input type="hidden" name="productID" id="df_ProductID" />
        <input type="hidden" name="houseID" id="df_HouseID" />
    </form>
</div>

<script class="init">
    LocatedMenu("仓库管理", "单据查询");

    $("#ddlMethod").kendoDropDownList();

    $("#txtWarehouse").kendoDropDownList({
        optionLabel: {
            House: "请选择仓库...",
            ID:0
        },
        dataTextField: "House",
        dataValueField: "ID",
        dataSource: Whale.dataSource({
            url: "/Orders/Warehouse/Gets"
        })
    });

    $(".date").kendoDatePicker({
        culture: "zh-CHS",
        format: "yyyy年MM月dd日",
    });

    var allProducts = Whale.Remote.GetEntity({
        url: "/Products/Product/Gets",
        parameters: {
            pageSize: 0,
            productStatus: 1
        }
    });
    $("#txtProduct").kendoComboBox({
        placeholder: "请选择产品...",
        autoBind: false,
        filter: "contains",
        dataTextField: "Pinyin",
        template: "#=ProductName#",
        dataValueField: "ID",
        dataSource: allProducts,
        change: function (e) {
            if ($.isNumeric(this.value())) {
                var item = this.dataItem();
                $("#txtProduct").attr("data-value", item.ID);
                this.text(item.ComplexName);
            } else if (this.text() == "") {
                $("#txtProduct").attr("data-value", 0);
            }
        }
    })

    var allCustomer = Whale.Remote.GetEntity({
        url: "/Members/Customer/Gets",
        parameters: {
            customerStatus: 1
        }
    });
    $("#txtCustomer").kendoComboBox({
        autoBind: false,
        placeholder: "请选择关联客户...",
        filter: "contains",
        dataTextField: "PinYinAndMobile",
        template: "#=ComplexName#",
        dataValueField: "ID",
        dataSource: allCustomer,
        change: function (e) {
            if ($.isNumeric(this.value())) {
                var item = this.dataItem();
                $("#txtCustomer").attr("data-value", item.ID);
                this.text(item.ComplexName);
            } else if (this.text() == "") {
                $("#txtCustomer").attr("data-value", 0);
            }
        }
    });

    function ExportOrders() {
        $("#df_OrderNo").val($("#txtKey").val());
        $("#df_CustomerID").val($("#txtCustomer").attr("data-value"));
        $("#df_OrderDateBefore").val($("#txtOrderDateBefore").val());
        $("#df_OrderDateAfter").val($("#txtOrderDateAfter").val());
        $("#df_OrderMethod").val($("#ddlMethod").data("kendoDropDownList").value());
        $("#df_ProductID").val($("#txtProduct").attr("data-value"));
        $("#df_HouseID").val($("#txtWarehouse").data("kendoDropDownList").value());
        $("#productForm").submit();
    }
</script>

<script class="grid_customer_columns">
    var ordersFields = [{
        title: "单据号",
        field: "OrderNo",
        template: "#=GetProductOrder(OrderNo)#",
        width: 140
    }, {
        title: "仓库",
        field: "Warehouse.House",
        width: 150
    }, {
        title: "关联交易单",
        field: "AssociatedDealOrder",
        template: "#=GetDealOrder(AssociatedDealOrder)#",
        width: 130
    }, {
        title: "日期",
        template: "#=kendo.toString(kendo.parseDate(NoteDate),'yyyy-MM-dd')#",
        width: 100
    }, {
        title: "备注",
        field: "Description",
        width: 200
    }, {}];

    function GetProductOrder(orderNo) {
        var html = "<a href=\"javascript:ShowProductOrder('" + orderNo + "')\">" + orderNo + "</a>";
        return html;
    }

    function ShowProductOrder(orderNo) {
        if (wndGeneralEdit != null && typeof wndGeneralEdit != "undefined") {
            wndGeneralEdit.setOptions({ title: "单据详情", width: 800 });
            wndGeneralEdit.refresh({ url: "/Orders/ProductOrder/Order/" + orderNo });
            wndGeneralEdit.open();
        }
    }

    function GetDealOrder(associatedDealOrder) {
        var html = "";
        if (associatedDealOrder != null)
            html += "<a href=\"javascript:ShowDealOrder('" + associatedDealOrder.OrderNo + "')\">" + associatedDealOrder.OrderNo + "</a>";
        return html;
    }

    function GetDealOrderNo(associatedDealOrderNo) {
        var html = "";
        if (associatedDealOrderNo != null)
            html += "<a href=\"javascript:ShowDealOrder('" + associatedDealOrderNo + "')\">" + associatedDealOrderNo + "</a>";
        return html;
    }

    function ShowDealOrder(orderNo) {
        if (wndGeneralEdit != null && typeof wndGeneralEdit != "undefined") {
            wndGeneralEdit.setOptions({ title: "单据详情", width: 800 });
            wndGeneralEdit.refresh({ url: "/Orders/DealOrder/Order/" + orderNo });
            wndGeneralEdit.open();
        }
    }
</script>

<script class="grid_config">
    $(function () {
        $("#grid_orders").kendoGrid({
            columns: ordersFields,
            dataSource: Whale.dataSource({
                url: "/Orders/ProductOrder/GetOrders",
                getParameters: function () {
                    return {
                        getHouse: true,
                        getDealOrder: true,
                        houseID: $("#txtWarehouse").data("kendoDropDownList").value(),
                        orderDateBefore: $("#txtOrderDateBefore").val(),
                        orderDateAfter: $("#txtOrderDateAfter").val(),
                        orderNo: $("#txtKey").val(),
                        method: $("#ddlMethod").data("kendoDropDownList").value(),
                        customerID: $("#txtCustomer").attr("data-value"),
                        productID: $("#txtProduct").attr("data-value")
                    };
                },
                pageSize: 20

            }),
            pageable: {
                messages: {
                    empty: "暂无数据"
                }
            },
            dataBound: function (e) {
                //让表头居中
                $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            }
        });
    });

    function FilterOrders() {
        $("#grid_orders").data("kendoGrid").dataSource.page(0);
        $("#grid_details").data("kendoGrid").dataSource.page(0);
    }
</script>

<script class="grid_details">
    var detailsFields = [{
        title: "单据号",
        field: "OrderNo",
        template: "#=GetProductOrder(OrderNo)#",
        width: 140
    }, {
        title: "关联交易单",
        field: "AssociatedDealNo",
        template: "#=GetDealOrderNo(AssociatedDealNo)#",
        width: 130
    }, {
        title: "客户",
        field: "Corporation",
        width: 200
    }, {
        title: "日期",
        template: "#=kendo.toString(kendo.parseDate(NoteDate),'yyyy-MM-dd')#",
        width: 100
    }, {
        title: "产品",
        field: "ProductName",
        width: 300
    }, {
        title: "数量",
        field: "Quantity",
        width: 100
    }, {
        title: "成本",
        field: "CostMoney",
        format: "{0:c}",
        width: 100,
        attributes: { style: "text-align:right" }
    }, {
        title: "批次",
        field: "Retention",
        width: 100
    }, {}];

    $(function () {
        $("#grid_details").kendoGrid({
            columns: detailsFields,
            dataSource: Whale.dataSource({
                url: "/Orders/ProductOrder/GetOrderDetails",
                getParameters: function () {
                    return {
                        houseID: $("#txtWarehouse").data("kendoDropDownList").value(),
                        orderDateBefore: $("#txtOrderDateBefore").val(),
                        orderDateAfter: $("#txtOrderDateAfter").val(),
                        orderNo: $("#txtKey").val(),
                        method: $("#ddlMethod").data("kendoDropDownList").value(),
                        customerID: $("#txtCustomer").attr("data-value"),
                        productID: $("#txtProduct").attr("data-value")
                    };
                },
                pageSize: 20
            }),
            pageable: {
                messages: {
                    empty: "暂无数据"
                }
            },
            dataBound: function (e) {
                //让表头居中
                $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            }
        });
    });
</script>
